/**
 * 
 */
package org.mspring.mlog.entity;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.lucene.document.Document;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.mspring.mlog.entity.security.User;
import org.mspring.mlog.search.Searchable;
import org.mspring.platform.cache.service.CacheService;

/**
 * 新闻资讯实体类
 * 
 * @author Gao Youbo
 * @since 2013年6月24日
 */
@Entity
@Table(name = "news")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class News implements Serializable, Searchable {

    private static final long serialVersionUID = 8645575670842042403L;

    private Long id; // 编号
    private NewsCatalog catalog;
    private String title; // 标题
    private String summary; // 描述
    private String content; // 内容
    private Date createTime; // 创建时间
    private User author; // 作者
    private String sourceUrl; // 来源站点URL
    private String sourceSite; // 来源站点名称
    private String status; // 状态
    
    private Long views;
    private Long comments;

    public News() {
    }

    public News(Long id) {
        super();
        this.id = id;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", unique = true, nullable = false, length = 30)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY, optional = true, targetEntity = NewsCatalog.class)
    @JoinColumn(name = "news_catalog")
    public NewsCatalog getCatalog() {
        return catalog;
    }

    public void setCatalog(NewsCatalog catalog) {
        this.catalog = catalog;
    }

    @Column(name = "title", unique = false, nullable = false, length = 200)
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Column(name = "summary", columnDefinition = "text")
    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    @Column(name = "content", nullable = false, columnDefinition = "text")
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Column(name = "create_time", length = 100)
    @Temporal(TemporalType.TIMESTAMP)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheService.CacheName.LAZY_CACHE_NAME)
    @ManyToOne(fetch = FetchType.EAGER, optional = true, targetEntity = User.class)
    @JoinColumn(name = "author")
    public User getAuthor() {
        return author;
    }

    public void setAuthor(User author) {
        this.author = author;
    }

    @Column(name = "source_url", length = 300)
    public String getSourceUrl() {
        return sourceUrl;
    }

    public void setSourceUrl(String sourceUrl) {
        this.sourceUrl = sourceUrl;
    }

    @Column(name = "source_site", length = 300)
    public String getSourceSite() {
        return sourceSite;
    }

    public void setSourceSite(String sourceSite) {
        this.sourceSite = sourceSite;
    }

    @Column(name = "status", length = 50)
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }
    
    @Column(name = "views", length = 30)
    public Long getViews() {
        return views;
    }

    public void setViews(Long views) {
        this.views = views;
    }

    @Column(name = "comments", length = 30)
    public Long getComments() {
        return comments;
    }

    public void setComments(Long comments) {
        this.comments = comments;
    }

    public static class Status {
        public static final String PUBLISH = "publish"; // 已发布的
        public static final String DRAFT = "draft"; // 草稿
        public static final String TRASH = "trash"; // 废弃的
        public static final String WAIT_FOR_APPROVE = "wait_for_approve"; // 等待审核
        public static final List<String> STATUS = Arrays.asList(new String[] { PUBLISH, DRAFT, TRASH, WAIT_FOR_APPROVE });

        public static final Map<String, String> getStatusMap() {
            Map<String, String> map = new HashMap<String, String>();
            map.put(PUBLISH, "已发布");
            map.put(DRAFT, "草稿");
            map.put(TRASH, "回收站");
            map.put(WAIT_FOR_APPROVE, "待审核");
            return map;
        }
    }

    // /////////////////////////////////////////////////////////////////////////
    // Search
    // /////////////////////////////////////////////////////////////////////////

    @Override
    public Long documentId() {
        // TODO Auto-generated method stub
        return id;
    }

    @Override
    public void documentId(Long id) {
        // TODO Auto-generated method stub
        this.id = id;
    }

    @Override
    public List<String> storeFields() {
        // TODO Auto-generated method stub
        return Arrays.asList("id", "title", "content", "createTime");
    }

    @Override
    public List<String> indexFields() {
        // TODO Auto-generated method stub
        return Arrays.asList("id", "title", "summary", "content", "status");
    }

    @Override
    public float boost() {
        // TODO Auto-generated method stub
        return 1.0f;
    }

    @Override
    public Map<String, String> extendStoreDatas() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Map<String, String> extendIndexDatas() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Searchable document2Object(Document doc) {
        // TODO Auto-generated method stub
        if (doc == null) {
            return null;
        }
        String idString = doc.get("id");
        String titleString = doc.get("title");
        String contentString = doc.get("content");
        String createTimeString = doc.get("createTime");
        News news = new News();
        news.setId(Long.valueOf(idString));
        news.setTitle(titleString);
        news.setContent(contentString);
        news.setCreateTime(new Date(Long.valueOf(createTimeString)));
        return news;
    }

}